
package com.scalar.chiptrack.operations.process.actions;


import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.operations.process.dao.FabDAO;
import com.scalar.chiptrack.operations.process.dao.FabStage;
import com.scalar.chiptrack.operations.process.dao.FabSubStage;
import com.scalar.chiptrack.operations.process.forms.FabStageForm;





public final class FabSubStageAction extends ChipTrackAction {

    public FabSubStageAction() {
    }

    public ActionForward execute (ActionMapping mapping, 
                                  ActionForm form, 
                                  HttpServletRequest request, 
                                  HttpServletResponse response) throws Exception {

			 HttpSession ses = request.getSession(true);

			FabStageForm fabStageForm = (FabStageForm) form;

			 		
			 	
			 int []  stageId			= fabStageForm.getStageId(); 	
			 String []  fabStepName		= fabStageForm.getFabStageName();
			 //int []  procedureDoc		= fabStageForm.getProcedureDoc();
			 String []  comments		= fabStageForm.getComments();
			 int []  order				= fabStageForm.getOrder();
			 String [] type				= fabStageForm.getType();
			 
			 FabStage fabStage = null;
			 ArrayList fabStageList = new ArrayList();
			 	
			String action=request.getParameter("action");
			String fabStageId=request.getParameter("id");	


			//System.out.println("fabStepName "+fabStepName.length);

			 for(int i=0;i<fabStepName.length;i++){
			  	fabStage=new FabStage();
				if(action.equals("new"))
				 {
				   fabStage.setType("new");
				   fabStage.setStageId(i+1);

				 }
				else{
				fabStage.setType("exists");
				fabStage.setStageId(stageId[i]);
				}
				fabStage.setFabStageName(fabStepName[i]);
				fabStage.setOrder(order[i]);
				//fabStage.setProcedureDoc(procedureDoc[i]);
				fabStage.setComments(comments[i]);
				
				fabStageList.add(fabStage);
																
			
			 }

			ses.setAttribute("fabStageList",fabStageList);

		
			request.setAttribute("fabStageId",fabStageId);
			//System.out.println("fabStageId "+fabStageId);	
			
			ArrayList subStageList = new ArrayList();

		if(action.equals("new"))
			{
					request.setAttribute("type","new");

				//System.out.println("In New");
				if(ses.getAttribute(fabStageId)==null)
				{
			
				FabSubStage fabSubStage = new FabSubStage();
				subStageList.add(fabSubStage);	
				request.setAttribute("subStageList",subStageList);
			
				}
				else{
				request.setAttribute("subStageList",ses.getAttribute(fabStageId));
				}
				
			}	
			
			// Code for exists 

			else{
					
			if(ses.getAttribute(fabStageId)== null){
		
			subStageList = FabDAO.getFabSubStage(fabStageId);
			request.setAttribute("subStageList",subStageList);
			}
			else{
					request.setAttribute("subStageList",ses.getAttribute(fabStageId));
				}
			request.setAttribute("type","exists");
				
		}
			 return (mapping.findForward("success")); 
	}
	
}